﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace ANGFStart
{
    static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            for (; ; )
            {
                AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
                setup.ApplicationName = "ANGF" + Guid.NewGuid().ToString();

                //string basePath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86);
                //if( string.IsNullOrEmpty(basePath) ) 
                //{
                //    basePath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
                //}

                var list = new List<string>();
                int num = setup.ApplicationBase.Length;
                foreach (var n in Directory.GetDirectories(setup.ApplicationBase, "*.*", SearchOption.AllDirectories))
                {
                    System.Diagnostics.Trace.Assert(n.StartsWith(setup.ApplicationBase), "case 1"); // 安全のため念のため
                    string newPath = n.Substring(num);
                    System.Diagnostics.Trace.Assert(!newPath.Contains(';'), "case 2"); // 安全のため念のため。セパレータと同じ文字は使えない
                    list.Add(newPath);
                }

#if RUNTIME
            string mainName = "ANGF.exe";
#elif TRIAL
            string mainName = "ANGF.exe";
#elif DEBUG
                string mainName = "ANGF.exe";
#elif UPGRADE
            string mainName = "ANGF.exe";
#else
            string mainName = "ANGFD.exe";
#endif
                setup.PrivateBinPath = string.Join(";", list);
                AppDomain sampleDomain = AppDomain.CreateDomain("ANGF"+Guid.NewGuid().ToString(), AppDomain.CurrentDomain.Evidence, setup);
                var executionPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string executionPath1 = Path.Combine(executionPath, mainName);
                if (File.Exists(executionPath1))
                {
                    int result = sampleDomain.ExecuteAssembly(executionPath1, args);
                    if (result == 1)
                    {
                        args = new string[0];
                        continue;
                    }
                    return;
                }
                // テストプロジェクト管理下で起動する場合はangf.exeオンリー
#if DEBUG
                string relpath = @"..\..\..\ANGF\bin\debug";
#elif TRIAL
            string relpath = @"..\..\..\ANGF\bin\release trial";
#elif UPGRADE
            string relpath = @"..\..\..\ANGF\bin\release upgrade";
#elif RUNTIME
            string relpath = @"..\..\..\ANGF\bin\release runtime";
#else
            string relpath = @"..\..\..\ANGF\bin\release";
#endif
                string mainName2 = "ANGF.exe";
                string executionPath2 = Path.Combine(Path.Combine(executionPath, relpath), mainName2);
                if (File.Exists(executionPath2))
                {
                    int result = sampleDomain.ExecuteAssembly(executionPath2, args);
                    if (result == 1)
                    {
                        args = new string[0];
                        continue;
                    }
                    return;
                }
                MessageBox.Show(mainName + " not found");
                //Application.EnableVisualStyles();
                //Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new Form1());
                break;
            }
        }
    }
}
